kiran wrote:
hi,
iam creating a multiselect menu in javascript the code for that is as
follows
"<select id=\"groups\" name=\"groups[]\" size=\"4\" multiple>"
"<option value=x>xyz</option>"
.
.
.
.
"</select>"
iam doing this completely in javascript. Its a dynamic menu. So how can
retrieve the values in javascript itself . if anyone knows kindly let
me know
try this:
<script>
var dynamicOpts = new Array(
new Option("xyz", "x"),
new Option("yzx", "y"),
new Option("zxy", "z"),
new Option("foo", "f"),
new Option("bar", "r"),
new Option("baz", "b")
);
function makeSelect(){
var theSelect = window.document.createElement("select");
arguments.callee.theSelect = theSelect;
theSelect.id = "groups";
theSelect.name = "groups[]";
theSelect.size = "4";
theSelect.multiple = "multiple";
var theOptions = theSelect.options;
for(var i=0;i<dynamicOpts.length;i++){
theOptions[theOptions.length] = dynamicOpts[i];
}
document.getElementById("genContFrm").appendChild( theSelect);
}
function showSelected(){
var opts = makeSelect.theSelect.options;
var selVals = "";
for(var i=0; i<opts.length;i++){
if(opts[i].selected){
selVals += opts[i].value + ",";
}
}
alert(selVals);
}
window.onload = makeSelect;
</script>
<form id="genContFrm">
<input type="button" onclick="showSelected()" value="show selected" />
</form>